package de.baumann.browser.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import de.baumann.browser.R
import jnr.ffi.annotations.In

class MnemonicAdapter(var context: Context,
                      var rvItemClickListener: RvItemClickListener?,
                      var itemCloseListener: RvItemClickListener?) : RecyclerView.Adapter<MnemonicAdapter.MnemonicViewHolder>() {

    var mnemonicArr: ArrayList<String>? = null
        set(value) {
            field = value
            if (value != null) {
                notifyDataSetChanged()
            }
        }

    private var clickedPosition = arrayListOf<Int>()
    fun addClickedPosition(position: Int) {
        clickedPosition.add(position)
        notifyDataSetChanged()
    }

    fun removePosition() {
        clickedPosition.removeAt(clickedPosition.size - 1)
        notifyDataSetChanged()
    }

    var errorPosition = -1
    fun mnemonicError() {
        errorPosition = mnemonicArr!!.size - 1
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MnemonicViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_mnemonic, parent, false)
        return MnemonicViewHolder(view)
    }

    override fun getItemCount(): Int {
        return if (mnemonicArr == null) {
            0
        } else {
            mnemonicArr!!.size
        }
    }

    override fun onBindViewHolder(holder: MnemonicViewHolder, position: Int) {
        holder.tvMnemonic.text = mnemonicArr!![position]
        holder.tvPosition.text = (position + 1).toString()

        if (errorPosition == position) {
            holder.ivClose.visibility = View.VISIBLE
        } else {
            holder.ivClose.visibility = View.GONE
        }

        if (clickedPosition.contains(position)) {
            holder.itemView.isClickable = false
            holder.tvMnemonic.setTextColor(context.resources.getColor(R.color.color_999999))
        } else {
            holder.itemView.isClickable = true
            holder.tvMnemonic.setTextColor(context.resources.getColor(R.color.windowLight))
        }
    }

    inner class MnemonicViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvMnemonic: TextView = itemView.findViewById(R.id.tvMnemonic)
        var tvPosition: TextView = itemView.findViewById(R.id.tvPosition)
        var ivClose: ImageView = itemView.findViewById(R.id.ivClose)

        init {
            if (rvItemClickListener != null) {
                itemView.setOnClickListener {
                    rvItemClickListener!!.onClick(layoutPosition)
                }
            }

            ivClose.setOnClickListener {
                errorPosition = -1
                itemCloseListener!!.onClick(layoutPosition)
                val lastElement = mnemonicArr!!.last()
                mnemonicArr!!.remove(lastElement)
                notifyDataSetChanged()
            }
        }
    }
}